package com.jinxin.platform.cdcardpassmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdcardpassmgr.bean.CdvehicleopersysOrgCorres;
import com.jinxin.platform.cdcardpassmgr.common.SysOrgInfo;
import com.jinxin.platform.cdcardpassmgr.mapper.CdvehicleopersysOrgCorresMapper;
import com.jinxin.platform.cdcardpassmgr.rpc.CdcardpassmgrRpc;
import com.jinxin.platform.cdcardpassmgr.service.ICdvehicleopersysOrgCorresService;
import com.jinxin.platform.cdcardpassmgr.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-06-30
 * @Author: Lingnan
 **/
@Service
public class CdvehicleopersysOrgCorresServiceImpl implements ICdvehicleopersysOrgCorresService {
    @Autowired
    private CdvehicleopersysOrgCorresMapper orgCorresMapper;
    @Resource
    private IdWorker idWorker;
    @Autowired
    private CdcardpassmgrRpc cdvehicleopersysRpc;

    @Override
    public CdvehicleopersysOrgCorres selectOne(HashMap param) {
        return orgCorresMapper.selectOne(param);
    }

    @Override
    public List<CdvehicleopersysOrgCorres> list(HashMap param) {
        return orgCorresMapper.list(param);
    }

    @Override
    public JsonResult saveOrUpdate(CdvehicleopersysOrgCorres record) {
        if (record == null || StringUtils.isEmpty(record.getThirdOrgId()) || StringUtils.isEmpty(record.getOrgId())){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count = 0;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            HashMap param = new HashMap();
            String thirdOrgId = record.getThirdOrgId();
            String orgId = record.getOrgId();
            param.put("thirdOrgId", thirdOrgId);
            param.put("orgId", orgId);
            CdvehicleopersysOrgCorres orgCorres = orgCorresMapper.selectOne(param);
            if(orgCorres == null){
                record.setId(String.valueOf(idWorker.nextId()));
                count = orgCorresMapper.insert(record);
            }
            List<SysOrgInfo> sysOrgInfos = cdvehicleopersysRpc.listOrganizeByParentId(orgId);
            if(sysOrgInfos != null && sysOrgInfos.size() > 0){
                sysOrgInfos.forEach(item->{
                    HashMap param2 = new HashMap();
                    param2.put("thirdOrgId", thirdOrgId);
                    String orgId2 = item.getOrgId();
                    param2.put("orgId", orgId2);
                    CdvehicleopersysOrgCorres orgCorres2 = orgCorresMapper.selectOne(param);
                    if(orgCorres2 == null){
                        CdvehicleopersysOrgCorres bean = new CdvehicleopersysOrgCorres();
                        bean.setId(String.valueOf(idWorker.nextId()));
                        bean.setThirdOrgId(thirdOrgId);
                        bean.setThirdOrgName(record.getThirdOrgName());
                        bean.setOrgId(orgId2);
                        bean.setOrgName(item.getOrgName());
                        orgCorresMapper.insert(bean);
                    }
                });
            }
            msg = "绑定成功";
        }else {
            count = orgCorresMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.ok("绑定失败或已绑定","");
        }
    }

    @Override
    public List<CdvehicleopersysOrgCorres> listByOrgIdList(List<String> orgIdList) {
        return orgCorresMapper.listByOrgIdList(orgIdList);
    }
}
